﻿using AvaVSExt.Attributes;
using AvaVSExt.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;

namespace AvaVSExt
{
    public partial class Program
    {
        [VSCommand("detemp")]
        public static void RunDetemplate()
        {
            var info = VSHelper.GetSelectedItemInfoInSolutionExplorer();
            if (info is null) return;
            if (info.ItemType != ItemType.Folder) return;

            var files = Directory.GetFiles(info.ItemFullPath, "*.*", SearchOption.AllDirectories);



            foreach (var file in files)
            {
                if (Detemplate.IsCodeFile(file)) 
                {
                    Detemplate.DetemplateIt(file);
                }
            }
        }


    }

    public class Detemplate
    {
        public static bool IsCodeFile(string file)
        {
            string[] allowExt = new string[]
             {
               "cs", "axaml", "xaml", "xml"
             };
            foreach (var ext in allowExt)
            {
                if (file.EndsWith(ext)) return true;
            }
            return false;
        }

        public static void DetemplateIt(string file)
        {
            var text = File.ReadAllText(file);

            text = text.Replace("Template.", string.Empty);
            text = text.Replace("template-", string.Empty);

            File.WriteAllText(file, text);
        }
    }
}
